app.controller("readBookCtrl",function($scope,$state,$sce,$http,$stateParams,$ionicLoading){
    $scope.isFirst = false;
    //保存本页内容
    var chapter = JSON.parse(localStorage.getItem("chapter"));
    var user_info = JSON.parse(localStorage.getItem("UserInfo"));
    // var getPath = JSON.parse(localStorage.getItem("getPath"));

    var cardInfo;
    var start_time = {};
    //进入页面初始化
    angular.element(document).ready(function () {
        //数据加载
        if($stateParams.cardNumber && $stateParams.taskDate){
            $scope.getCard($stateParams.cardNumber,$stateParams.taskDate);
        }else{
            $scope.getCard(chapter.cardNumber,chapter.taskDate);
        }
    });
    $scope.getCard = function (cardNumber,taskDate) {
        $ionicLoading.show({
            template: "数据加载中...",
        })
        var url = getPath.url+getPath.name+"/app/findCardByNumber";
        var param = {
            userId:user_info.userId,
            cardNumber:cardNumber.toString(),
            taskDate:taskDate,
        }
        $http({
            url: url,
            method: "POST",
            data:param,
            headers:{'Content-Type':'application/x-www-form-urlencoded'}
        }).success(function(data){
                var res = data.resultBean.cardList[0];
                chapter = res;
                cardInfo = data.resultBean.cycleDetailCardDTO;
                //判断该卡片是否已经阅读过，如果已经阅读过不保存阅读时间
                if(cardInfo.isReaded==0){
                    //记录当前时间
                    var now = new Date().Format("yyyy-MM-dd hh:mm:ss");
                    start_time = {
                        bookId:user_info.bookId,
                        cardId:chapter.cardId,
                        start_time:now
                    }
                    // localStorage.setItem("start_time",JSON.stringify(start_time));
                }
                if(data.resultCode==200){
                    localStorage.setItem("chapter",JSON.stringify(chapter));
                    $scope.card_name = chapter.cardName;
                    $scope.cardReadRate = Math.round((cardInfo.cardReadRate*100)) || 0;
                    $scope.card_content = $sce.trustAsHtml(chapter.cardContent);
                }else{
                    $ionicLoading.show({
                        template: "获取章节内容失败！",
                        duration:3000
                    });
                }
                $ionicLoading.hide();
            })
            .error(function(data){
                $ionicLoading.show({
                    template: "获取章节内容失败！",
                    duration:3000
                });
            });
    }
    //上一章
    $scope.openPrev = function () {
        //修改获取卡片的顺序
        var prevCardNumber = chapter.cardNumber-1;
        var taskDate = chapter.taskDate;
        if(cardInfo.isReaded==0){
            $scope.readOver(prevCardNumber,taskDate);
        }else{
            if(cardInfo.minCardNumber==chapter.cardNumber){
                //重新加载此页面
                $state.go("home.read-catalog-list");
            }else{
                //重新加载此页面
                $state.go("home.read-book",{
                    cardNumber:prevCardNumber,
                    taskDate:taskDate
                });
            }
        }


    };
    //下一章
    $scope.openNext = function () {
        //修改获取卡片的顺序
        var nextCardNumber = chapter.cardNumber+1;
        var taskDate = chapter.taskDate;
        //判断该卡片是否阅读过，如果已经阅读过，不保存阅读时间
        if(cardInfo.isReaded==0){
            //点击下一章保存此卡片阅读时间
            $scope.readOver(nextCardNumber,taskDate);
        }else{
            //如果已经是最后一页，跳转签到页面
            if(cardInfo.maxCardNumber==chapter.cardNumber){
                //判断是否签到过
                var user_info = JSON.parse(localStorage.getItem("UserInfo"));
                if(user_info.signStatus==0){
                    $state.go("home.read-book-punchcard",{
                        taskDate:taskDate
                    });
                }else{
                    $ionicLoading.show({
                        template: "当前章节已经是本日最后章节",
                        duration:3000
                    });
                }
            }else{
                //重新加载此页面
                $state.go("home.read-book",{
                    cardNumber:nextCardNumber,
                    taskDate:taskDate
                });
            }
        }

    };
    //保存当前卡片阅读时间
    $scope.readOver = function (nextCardNumber,taskDate) {
        //当前时间
        var now = new Date().Format("yyyy-MM-dd hh:mm:ss");
        //从缓存中取出开始时间
        // var start_time = JSON.parse(localStorage.getItem("start_time"));
        //为空时取当前时间
        if(!start_time){
            start_time = {
                bookId:user_info.bookId,
                cardId:chapter.cardId,
                start_time:now
            }
        }
        var url = getPath.url+getPath.name+"/app/saveReadCardTime";
        var param = {
            userId:user_info.userId,
            cardId:chapter.cardId,
            strStartTime:start_time.start_time,
            strEndTime:now
        }
        if($stateParams.taskDate){
            param.taskDay = $stateParams.taskDate;
        }else{
            param.taskDay = chapter.taskDate;
        }
        $http({
            url: url,
            method: "POST",
            data:param,
            headers:{'Content-Type':'application/x-www-form-urlencoded'}
        }).success(function(data){
            var res = data.resultBean;
            if(data.resultCode==200){
                //清除缓存阅读时间
                // localStorage.removeItem("start_time");
                start_time = {};
                //如果已经是最后一页，跳转签到页面
                if(cardInfo.maxCardNumber==chapter.cardNumber){
                    //判断是否签到过
                    var user_info = JSON.parse(localStorage.getItem("UserInfo"));
                    if(user_info.signStatus==0){
                        $state.go("home.read-book-punchcard",{
                            taskDate:taskDate
                        });
                    }else{
                        $ionicLoading.show({
                            template: "当前章节已经是本日最后章节",
                            duration:3000
                        });
                    }
                }else{
                    //重新加载此页面
                    $state.go("home.read-book",{
                        cardNumber:nextCardNumber,
                        taskDate:taskDate
                    });
                }
            }
            // else{
            //     $ionicLoading.show({
            //         template: "保存阅读时间失败",
            //         duration:3000
            //     });
            // }
        })
        // .error(function(){
        //     $ionicLoading.show({
        //         template: "保存阅读时间失败",
        //         duration:3000
        //     });
        // });
    }
    //添加笔记
    $scope.openNoteAdd = function(){
        var noteData = {
            noteBookId:user_info.bookId,
            noteChapterId:chapter.chapterId,
            noteCardId:chapter.cardId,
            noteCardName:chapter.cardName
        }
        localStorage.setItem("noteData",JSON.stringify(noteData));
        $state.go("home.noteAdd");
    }
    //返回列表
    $scope.goBackCatalog = function () {
        $state.go("home.read-catalog");
    }
});